﻿using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using Resolve.Contracts;

namespace Resolve.Web
{
    public abstract class BasePage : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 
        /// </summary>
        protected HttpContext Context;

        /// <summary>
        /// 
        /// </summary>
        protected HttpResponse Response;

        /// <summary>
        /// 
        /// </summary>
        protected HttpRequest Request;

        /// <summary>
        /// 
        /// </summary>
        protected HttpServerUtility Server;

        /// <summary>
        /// 模型数据
        /// </summary>
        protected readonly IDictionary<string, object> Model = new Dictionary<string, object>();

        /// <summary>
        /// 视图数据
        /// </summary>
        protected readonly IDictionary<string, object> ViewData = new Dictionary<string, object>();

        /// <summary>
        /// 关联Html文件
        /// </summary>
        private string _filePath;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        public void ProcessRequest(HttpContext httpContext)
        {
            Context = httpContext;
            Response = httpContext.Response;
            Request = httpContext.Request;
            Server = httpContext.Server;
            _filePath = GetFilePath();
            PageLoad();
            Render();
        }

        /// <summary>
        /// 页面逻辑处理
        /// </summary>
        protected abstract void PageLoad();

        /// <summary>
        /// 渲染Html
        /// </summary>
        private void Render()
        {
            //ResolveEngineBase resolveEngine = new ResolveEngineForFile();
            ResolveEngineBase resolveEngine = new ResolveEngineForFolder();
            _filePath = "~" + _filePath;
            resolveEngine.ViewData = ViewData;
            resolveEngine.Model = Model;
            string result = resolveEngine.Resolve(Server.MapPath(_filePath));
            Response.Write(result);
        }

        /// <summary>
        /// 获取关联文件路径
        /// </summary>
        /// <returns></returns>
        protected virtual string GetFilePath()
        {
            string path;
            string requestPath = Request.FilePath;
            int index = requestPath.LastIndexOf(".", System.StringComparison.Ordinal);
            if (index > -1)
            {
                path = "/Template" + requestPath.Substring(0, index) + "." + ResolveConfig.TemplateExe;
            }
            else
            {
                path = "/Template" + requestPath + "." + ResolveConfig.TemplateExe;
            }
            return path;
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
